Titanium.include("/db/db.js");
var win = Titanium.UI.currentWindow;
win.backgroundColor = '#13386c';
win.barColor = '#13386c';

//
//  CREATE FIELD ONE
//
var email = Titanium.UI.createLabel({
	color:'#fff',
	text:'Email',
	top:10,
	left:30,
	width:100,
	height:40
});

win.add(email);

var emailField = Titanium.UI.createTextField({
	hintText:'Enter Email',
	height:40,
	top:55,
	left:30,
	width:250,
	borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

win.add(emailField);


var password = Titanium.UI.createLabel({
	color:'#fff',
	text:'Password',
	top:100,
	left:30,
	width:100,
	height:40
});

win.add(password);

var pwdField = Titanium.UI.createTextField({
	hintText:'enter password',
	
	height:40,
	top:145,
	left:30,
	width:250,
	borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

win.add(pwdField);

var type = Titanium.UI.createLabel({
	color:'#fff',
	text:'Are you student or teacher?',
	top:190,
	left:30,
	width:300,
	height:40
});

win.add(type);

var typeField = Titanium.UI.createTextField({
	hintText:'Student or Teacher',
	
	height:40,
	top:235,
	left:30,
	width:250,
	borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

win.add(typeField);


//
// CREATE BUTTON
//
var save = Titanium.UI.createButton({
	title:'Save',
	top:300,
	left:30,
	height:40,
	width:250
});
win.add(save);


var forgot = Titanium.UI.createLabel({
	color:'#fff',
	text:'Forgot Password?',
	top:345,
	left:130,
	width:300,
	height:40
});

win.add(forgot);

save.addEventListener('click',function()
{
	//Check the fields
	if((emailField.value!="" && emailField.value!=null) || (pwdField.value!="" && pwdField.value!=null) || (typeField.value!="" && typeField.value!=null)){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;    
	   if(reg.test(emailField.value) == false) {
	        var a = Titanium.UI.createAlertDialog({
				title:'Alert',
				message:'Please provide valid email address.'
			});
			a.show();
	    } else {
	    	var typeUpper = typeField.value.toUpperCase();
	    	if(typeUpper!="STUDENT" && typeUpper!="TEACHER"){
	    		var a = Titanium.UI.createAlertDialog({
				title:'Alert',
				message:'Please use Student or Teacher values only.'
				});
				a.show();
	    	}else{
		    	//Save the info to the backend
		    	var data =[];
		    	data[0] =emailField.value;
		    	data[1] =typeField.value;
		    	data[2] =pwdField.value;
		    	var userCreated = createUser(data);
		    	//if(userCreated){
			    	//Store username in App settings
			        Titanium.App.Properties.setString("username", emailField.value);
			        Titanium.App.Properties.setString("password", pwdField.value);
			        Titanium.App.Properties.setString("userType", typeField.value);
			        //Take user back to Dashboard
			        
					var dashBoard=Titanium.UI.createWindow({
				    	url:'../app.js'
					});
					dashBoard.open(); 
					//TODO Add sync call for MongoDB inserts.
				// }else{
					// var a = Titanium.UI.createAlertDialog({
						// title:'Alert',
						// message:'Sorry we couldn\'t create an account for you. Plz try later.'
					// });
					// a.show();
				// } 
			}    
	    }
		
	}else{
		var a = Titanium.UI.createAlertDialog({
			title:'Alert',
			message:'Please provide all information.'
		});
		a.show();
	}
});


